package com.ss.project.xia17user.rest.resource;

import com.ss.project.xia17user.common.utils.ResponseUtil;
import com.ss.project.xia17user.config.security.SecurityUtils;
import com.ss.project.xia17user.core.exceptions.BadRequestException;
import com.ss.project.xia17user.dao.entity.User;
import com.ss.project.xia17user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 用户资源
 * @author xia17
 * @since 2020/9/14 20:41
 */
@RequestMapping("oauth2/res")
@RestController
@RequiredArgsConstructor
@Slf4j
public class UserResourceRest {

    private final UserService userService;

    /**
     * 获取用户信息
     * @return /
     */
    @GetMapping("user-info")
    @PreAuthorize("@spc.hasOne('SCOPE_USER_INFO')")
    public User userInfo(){
        User user = userService.findByUsername(SecurityUtils.username());
        if (user == null){
            log.error("未找到用户名[{}]的用户信息",SecurityUtils.username());
            throw new BadRequestException();
        }
        return user;
    }


}
